Enter the directory of the maca folder on your drive and the name of the tissue you want to analyze.
tissue_of_interest = "Marrow"
Load the requisite packages and some additional helper functions.
library(here)
library(useful)
library(Seurat)
library(dplyr)
library(Matrix)
save_dir = here('00_data_ingest', 'tissue_robj')
# read the metadata to get the plates we want
plate_metadata_filename = here('00_data_ingest', 'facs_raw_data', 'metadata_FACS.csv')
plate_metadata <- read.csv(plate_metadata_filename, sep=",", header = TRUE)
colnames(plate_metadata)[1] <- "plate.barcode"
plate_metadata
Subset the metadata on the tissue.
tissue_plates = filter(plate_metadata, tissue == tissue_of_interest)[,c('plate.barcode','tissue','subtissue','mouse.sex')]
tissue_plates
Load the read count data.
#Load the gene names and set the metadata columns by opening the first file
filename = here('00_data_ingest', 'facs_raw_data', 'FACS', paste0(tissue_of_interest, '-counts.csv'))
raw.data = read.csv(filename, sep=",", row.names=1)
# raw.data = data.frame(row.names = rownames(raw.data))
corner(raw.data)
Make a vector of plate barcodes for each cell
plate.barcodes = lapply(colnames(raw.data), function(x) strsplit(strsplit(x, "_")[[1]][1], '.', fixed=TRUE)[[1]][2])
head(plate.barcodes)
[[1]]
[1] "D042044"
[[2]]
[1] "D042044"
[[3]]
[1] "D042044"
[[4]]
[1] "D042044"
[[5]]
[1] "D042044"
[[6]]
[1] "D042044"
Use only the metadata rows corresponding to Bladder plates. Make a plate barcode dataframe to “expand” the per-plate metadata to be per-cell.
barcode.df = t.data.frame(as.data.frame(plate.barcodes))
rownames(barcode.df) = colnames(raw.data)
colnames(barcode.df) = c('plate.barcode')
head(barcode.df)
plate.barcode
A22.D042044.3_9_M.1.1 "D042044"
C5.D042044.3_9_M.1.1 "D042044"
D10.D042044.3_9_M.1.1 "D042044"
E13.D042044.3_9_M.1.1 "D042044"
F19.D042044.3_9_M.1.1 "D042044"
H2.D042044.3_9_M.1.1 "D042044"
rnames = row.names(barcode.df)
meta.data <- merge(barcode.df, plate_metadata, by='plate.barcode', sort = F)
row.names(meta.data) <- rnames
# Sort cells by plate barcode because that's how the data was originally
meta.data = meta.data[order(meta.data$plate.barcode), ]
corner(meta.data)
raw.data = raw.data[, rownames(meta.data)]
corner(raw.data)
Process the raw data and load it into the Seurat object.
# Find ERCC's, compute the percent ERCC, and drop them from the raw data.
erccs <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = TRUE)
percent.ercc <- Matrix::colSums(raw.data[erccs, ])/Matrix::colSums(raw.data)
ercc.index <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = FALSE)
raw.data <- raw.data[-ercc.index,]
# Create the Seurat object with all the data
tiss <- CreateSeuratObject(raw.data = raw.data, project = tissue_of_interest,
min.cells = 5, min.genes = 5)
tiss <- AddMetaData(object = tiss, meta.data)
tiss <- AddMetaData(object = tiss, percent.ercc, col.name = "percent.ercc")
# Change default name for sums of counts from nUMI to nReads
colnames(tiss@meta.data)[colnames(tiss@meta.data) == 'nUMI'] <- 'nReads'
# Create metadata columns for annotations and subannotations
tiss@meta.data[,'annotation'] <- NA
tiss@meta.data[,'subannotation'] <- NA
Calculate percent ribosomal genes.
ribo.genes <- grep(pattern = "^Rp[sl][[:digit:]]", x = rownames(x = tiss@data), value = TRUE)
percent.ribo <- Matrix::colSums(tiss@raw.data[ribo.genes, ])/Matrix::colSums(tiss@raw.data)
tiss <- AddMetaData(object = tiss, metadata = percent.ribo, col.name = "percent.ribo")
A sanity check: genes per cell vs reads per cell.
GenePlot(object = tiss, gene1 = "nReads", gene2 = "nGene", use.raw=T)
Filter out cells with few reads and few genes.
tiss <- FilterCells(object = tiss, subset.names = c("nGene", "nReads"),
low.thresholds = c(500, 50000), high.thresholds = c(25000, 2000000))
Normalize the data, then regress out correlation with total reads
tiss <- NormalizeData(object = tiss)
Performing log-normalization
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
**************************************************|
tiss <- ScaleData(object = tiss, vars.to.regress = c("nReads", "percent.ribo","Rn45s"))
[1] "Regressing out nReads" "Regressing out percent.ribo" "Regressing out Rn45s"
|
| | 0%
|
|= | 1%
|
|== | 2%
|
|=== | 2%
|
|=== | 3%
|
|==== | 3%
|
|===== | 4%
|
|===== | 5%
|
|====== | 5%
|
|======= | 6%
|
|======== | 6%
|
|======== | 7%
|
|========= | 7%
|
|========== | 8%
|
|========== | 9%
|
|=========== | 9%
|
|============ | 10%
|
|============= | 11%
|
|============== | 11%
|
|============== | 12%
|
|=============== | 13%
|
|================ | 13%
|
|================ | 14%
|
|================= | 14%
|
|================== | 15%
|
|=================== | 15%
|
|=================== | 16%
|
|==================== | 17%
|
|===================== | 17%
|
|===================== | 18%
|
|====================== | 18%
|
|======================= | 19%
|
|======================== | 20%
|
|========================= | 21%
|
|========================== | 22%
|
|=========================== | 22%
|
|=========================== | 23%
|
|============================ | 23%
|
|============================= | 24%
|
|============================= | 25%
|
|============================== | 25%
|
|=============================== | 26%
|
|================================ | 26%
|
|================================ | 27%
|
|================================= | 27%
|
|================================== | 28%
|
|================================== | 29%
|
|=================================== | 29%
|
|==================================== | 30%
|
|===================================== | 31%
|
|====================================== | 31%
|
|====================================== | 32%
|
|======================================= | 33%
|
|======================================== | 33%
|
|======================================== | 34%
|
|========================================= | 34%
|
|========================================== | 35%
|
|=========================================== | 35%
|
|=========================================== | 36%
|
|============================================ | 37%
|
|============================================= | 37%
|
|============================================= | 38%
|
|============================================== | 38%
|
|=============================================== | 39%
|
|================================================ | 40%
|
|================================================= | 41%
|
|================================================== | 42%
|
|=================================================== | 42%
|
|=================================================== | 43%
|
|==================================================== | 43%
|
|===================================================== | 44%
|
|===================================================== | 45%
|
|====================================================== | 45%
|
|======================================================= | 46%
|
|======================================================== | 46%
|
|======================================================== | 47%
|
|========================================================= | 47%
|
|========================================================== | 48%
|
|========================================================== | 49%
|
|=========================================================== | 49%
|
|============================================================ | 50%
|
|============================================================= | 51%
|
|============================================================== | 51%
|
|============================================================== | 52%
|
|=============================================================== | 53%
|
|================================================================ | 53%
|
|================================================================ | 54%
|
|================================================================= | 54%
|
|================================================================== | 55%
|
|=================================================================== | 55%
|
|=================================================================== | 56%
|
|==================================================================== | 57%
|
|===================================================================== | 57%
|
|===================================================================== | 58%
|
|====================================================================== | 58%
|
|======================================================================= | 59%
|
|======================================================================== | 60%
|
|========================================================================= | 61%
|
|========================================================================== | 62%
|
|=========================================================================== | 62%
|
|=========================================================================== | 63%
|
|============================================================================ | 63%
|
|============================================================================= | 64%
|
|============================================================================= | 65%
|
|============================================================================== | 65%
|
|=============================================================================== | 66%
|
|================================================================================ | 66%
|
|================================================================================ | 67%
|
|================================================================================= | 67%
|
|================================================================================== | 68%
|
|================================================================================== | 69%
|
|=================================================================================== | 69%
|
|==================================================================================== | 70%
|
|===================================================================================== | 71%
|
|====================================================================================== | 71%
|
|====================================================================================== | 72%
|
|======================================================================================= | 73%
|
|======================================================================================== | 73%
|
|======================================================================================== | 74%
|
|========================================================================================= | 74%
|
|========================================================================================== | 75%
|
|=========================================================================================== | 75%
|
|=========================================================================================== | 76%
|
|============================================================================================ | 77%
|
|============================================================================================= | 77%
|
|============================================================================================= | 78%
|
|============================================================================================== | 78%
|
|=============================================================================================== | 79%
|
|================================================================================================ | 80%
|
|================================================================================================= | 81%
|
|================================================================================================== | 82%
|
|=================================================================================================== | 82%
|
|=================================================================================================== | 83%
|
|==================================================================================================== | 83%
|
|===================================================================================================== | 84%
|
|===================================================================================================== | 85%
|
|====================================================================================================== | 85%
|
|======================================================================================================= | 86%
|
|======================================================================================================== | 86%
|
|======================================================================================================== | 87%
|
|========================================================================================================= | 87%
|
|========================================================================================================== | 88%
|
|========================================================================================================== | 89%
|
|=========================================================================================================== | 89%
|
|============================================================================================================ | 90%
|
|============================================================================================================= | 91%
|
|============================================================================================================== | 91%
|
|============================================================================================================== | 92%
|
|=============================================================================================================== | 93%
|
|================================================================================================================ | 93%
|
|================================================================================================================ | 94%
|
|================================================================================================================= | 94%
|
|================================================================================================================== | 95%
|
|=================================================================================================================== | 95%
|
|=================================================================================================================== | 96%
|
|==================================================================================================================== | 97%
|
|===================================================================================================================== | 97%
|
|===================================================================================================================== | 98%
|
|====================================================================================================================== | 98%
|
|======================================================================================================================= | 99%
|
|========================================================================================================================| 100%
[1] "Scaling data matrix"
|
| | 0%
|
|======= | 6%
|
|============= | 11%
|
|==================== | 17%
|
|=========================== | 22%
|
|================================= | 28%
|
|======================================== | 33%
|
|=============================================== | 39%
|
|===================================================== | 44%
|
|============================================================ | 50%
|
|=================================================================== | 56%
|
|========================================================================= | 61%
|
|================================================================================ | 67%
|
|======================================================================================= | 72%
|
|============================================================================================= | 78%
|
|==================================================================================================== | 83%
|
|=========================================================================================================== | 89%
|
|================================================================================================================= | 94%
|
|========================================================================================================================| 100%
tiss <- FindVariableGenes(object = tiss, do.plot = TRUE, x.high.cutoff = Inf, y.cutoff = 0.5)
Calculating gene means
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
**************************************************|
Calculating gene variance to mean ratios
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
**************************************************|
Run Principal Component Analysis.
tiss <- RunPCA(object = tiss, do.print = FALSE)
tiss <- ProjectPCA(object = tiss, do.print = FALSE)
Later on (in FindClusters and TSNE) you will pick a number of principal components to use. This has the effect of keeping the major directions of variation in the data and, ideally, supressing noise. There is no correct answer to the number to use, but a decent rule of thumb is to go until the plot plateaus.
PCElbowPlot(object = tiss)
Choose the number of principal components to use.
# Set number of principal components.
n.pcs = 15
The clustering is performed based on a nearest neighbors graph. Cells that have similar expression will be joined together. The Louvain algorithm looks for groups of cells with high modularity–more connections within the group than between groups. The resolution parameter determines the scale…higher resolution will give more clusters, lower resolution will give fewer.
For the top-level clustering, aim to under-cluster instead of over-cluster. It will be easy to subset groups and further analyze them below.
# Set resolution
res.used <- 0.5
tiss <- FindClusters(object = tiss, reduction.type = "pca", dims.use = 1:n.pcs,
resolution = res.used, print.output = 0, save.SNN = TRUE)
To visualize
# If cells are too spread out, you can raise the perplexity. If you have few cells, try a lower perplexity (but never less than 10).
tiss <- RunTSNE(object = tiss, dims.use = 1:n.pcs, seed.use = 10, perplexity=84)
# note that you can set do.label=T to help label individual clusters
TSNEPlot(object = tiss, do.label = T)
Check expression of genes of interset.